package com.wx.service.impl;

import com.wx.entity.CollectDetailModel;
import com.wx.entity.CollectProductModel;
import com.wx.entity.CollectUserProductDetailModel;
import com.wx.entity.UserCollectModel;
import com.wx.mapper.UserProductDetailMapperCustom;
import com.wx.mapper.WxDepotMapper;
import com.wx.mapper.WxUserProductDetailMapper;
import com.wx.pojo.*;
import com.wx.service.*;
import com.wx.utils.BigDecimalUtil;
import com.wx.utils.DateTimeUtil;
import com.wx.utils.RMBUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserProductDetailServiceImpl implements IUserProductDetailService {

    @Autowired
    private WxUserProductDetailMapper wxUserProductDetailMapper;

    @Autowired
    private UserProductDetailMapperCustom userProductDetailMapperCustom;
    @Autowired
    private ICollectService iCollectService;
    @Autowired
    private IUserProductDetailService iUserProductDetailService;
    @Autowired
    private ICollectProductDetailService iCollectProductDetailService;
    @Autowired
    private IProductService iProductService;
    @Autowired
    private IOrderService iOrderService;
    @Autowired
    private WxDepotMapper wxDepotMapper;


    @Override
    public List<WxUserProductDetail> listUserProductDetail(){
        return wxUserProductDetailMapper.selectAll();
    }

    @Override
    public List<WxUserProductDetail> listModelByNumber(String collect_number){
        List<WxUserProductDetail> list=userProductDetailMapperCustom.listModelByNumber(collect_number);
        return userProductDetailMapperCustom.listModelByNumber(collect_number);
    }

    @Override
    public List<WxUserProductDetail> listModelByNumberAndUserId(String collect_number,
                                                                String user_id){
        return userProductDetailMapperCustom.listModelByNumberAndUserId(collect_number,user_id);
    }

    /*
    获取用户记录
     */
    @Override
    public List<UserCollectModel> listModelJoinUser(String collect_number){

        List<UserCollectModel> list=new ArrayList<>();
        for (WxUserProductDetail item:userProductDetailMapperCustom.listModelJoinUser(collect_number)) {
            UserCollectModel model=new UserCollectModel();
            model.setName(item.getPrep());
            model.setProname(item.getProductName());
            model.setAmount(item.getAmount().toString());
            model.setTime(DateTimeUtil.dateToStr(item.getGmtCreate()));
            list.add(model);
        }
        return list;
    }

    @Override
    public List<WxUserProductDetail> listDistinctModel(String collect_number,
                                                          Integer user_id,
                                                          Integer product_id){
        return userProductDetailMapperCustom.listDistinctModel(
                collect_number,
                user_id,
                product_id);
    }

    @Override
    public List<String> listDistinctModelByUser(Integer user_id){

        List<WxUserProductDetail> list= userProductDetailMapperCustom.listDistinctModelByUser(user_id);
        List<String> list1=new ArrayList<>();
        for (WxUserProductDetail str:list) {
            list1.add(str.getCollectNumber());
        }
        return list1;
    }


    /**
     * 用户参与集采活动 增加明细
     * @param user_id 用户id
     * @param number 集采编号
     * @return
     */
    @Override
    public Boolean participateModel(Integer user_id,
                                    String number){

        //判断用户是否已经参与集采活动 已经参与直接返回
        List<WxUserProductDetail> liss=iUserProductDetailService.listDistinctModel(number,user_id,null);
        if (liss.size()>0){
            if (iUserProductDetailService.listDistinctModel(number,user_id,null).get(0).getAmount()>0){
                return false;
            }
        }


        //获取集采 model
        WxCollect collectModel=iCollectService.getModelByNumber(number);

        //遍历集采活动商品 增加用户购买商品的明细
        for (WxCollectProductDetail item : iCollectProductDetailService.listModelByNumber(number)) {

            //获取 产品 Model
            WxProduct pModel=iProductService.getModelById(item.getProductId());

            //填充 用户购买商品明细记录
            WxUserProductDetail model=new WxUserProductDetail();
            model.setCollectUserId(1);
            model.setProductId(item.getProductId());
            model.setCollectNumber(number);
            model.setUserId(user_id);
            model.setAmount(0);  //默认购买数量为o
            model.setPrice(collectModel.getStartPrice());
            model.setSubtotal(new BigDecimal(0));
            model.setLprice(collectModel.getLatestPrice());
            model.setBrand(pModel.getBrand());
            model.setProductName(pModel.getName());
            model.setPrep("");

            //生成 用户购买商品明细记录
            userProductDetailMapperCustom.insertModel(model);
        }
        return true;
    }

    /*
    修改产品明细的数量
     */
    @Override
    public Integer updateModelAmount(WxUserProductDetail model){
        return userProductDetailMapperCustom.updateModelAmount(model);
    }

    /**
     * 获取集采详情列表
     * @param user_id 用户ID
     * @param status 集采状态 1 正在集采 2已经结束
     * @param verify 身份认证 1 全部集采 2订单  9是个人集采列表
     * @return
     */
    @Override
    public List<CollectDetailModel> listModelDetail(Integer user_id,Integer status,Integer verify){

        //集采列表
        List<CollectDetailModel> list =new ArrayList<>();
        if (verify==1){
            for (WxCollect wxCollect:iCollectService.listModelByStatus(status)) {
                list.add(iUserProductDetailService.getModelDetail(wxCollect.getNumber(),user_id));
            }
        }else if (verify==2){
            for (WxCollect wxCollect:iCollectService.listCollect()) {
                //查询用户参与的订单状态在运送中的所有集采活动
                List<String> list1=iOrderService.listDistinctModelByUserAndStatus(user_id,status);
                if (list1.contains(wxCollect.getNumber())){
                    list.add(iUserProductDetailService.getModelDetail(wxCollect.getNumber(),user_id));
                }
            }
        }
        else {
            for (WxCollect wxCollect:iCollectService.listModelByStatus(status)) {
                //查询用户参与的所有集采活动
                List<String> list1=iUserProductDetailService.listDistinctModelByUser(user_id);
                if (list1.contains(wxCollect.getNumber())){
                    list.add(iUserProductDetailService.getModelDetail(wxCollect.getNumber(),user_id));
                }
            }
        }
        return list;
    }

    /**
     * 获取用户参与集采活动明细
     * @param collect_number 集采编号
     * @param user_id  用户id
     * @return
     */
    @Override
    public CollectDetailModel getModelDetail(String collect_number,Integer user_id){

        Integer myamounts=0;
        String  newPrices="";
        Integer myTotalPrice=0;

        //集采活动Model
        WxCollect collectModel=iCollectService.getModelByNumber(collect_number);

        WxDepot deModel=wxDepotMapper.selectByPrimaryKey(collectModel.getDepotId());

        WxOrder orderModel=iOrderService.getModel(collect_number,user_id);

        // 自定义返回 集采活动 Model
        CollectDetailModel model=new CollectDetailModel();
        // 集采活动 产品列表
        List<CollectProductModel> listProduct=new ArrayList<>();
        //集采活动 产品明细列表
        List<CollectUserProductDetailModel> list1 =new ArrayList<>();
        // 返回集采活动产品明细Model
        for (WxUserProductDetail item: iUserProductDetailService.listDistinctModel(collect_number,null,null)) {

            model.setBrand(iUserProductDetailService.listDistinctModel(collect_number,user_id,item.getProductId()).get(0).getBrand());

            //集采活动产品明细 Model
            CollectUserProductDetailModel model1=new CollectUserProductDetailModel();

            model1.setId(item.getProductId());
            model1.setName(item.getProductName());
            model1.setBrand(item.getBrand());
            model1.sethPrice(item.getPrice());
            model1.setlPrice(item.getLprice());
            model1.setSpread(new BigDecimal(item.getPrep()));
            model1.setAmount(item.getAmount());
            model1.setMyAmount(iUserProductDetailService.listDistinctModel(collect_number,user_id,item.getProductId()).get(0).getAmount()); ;
            list1.add(model1);

            CollectProductModel model2=new CollectProductModel();
            model2.setProduct_name(item.getProductName());
            listProduct.add(model2);

            //计算我总共购买量 和最新价格 以及我总共花的钱
            myamounts +=model1.getMyAmount();
            newPrices =item.getLprice().toString();
            myTotalPrice +=model1.getMyAmount() * model1.getlPrice().intValue();
        }

        // 填充 集采活动明细 Model
        model.setCity(collectModel.getCity());
        model.setNumber(collect_number);
        model.setAmount(collectModel.getAmount());
        model.setTotalPrice(userProductDetailMapperCustom.countPriceByNumber(collect_number)==null?"0":userProductDetailMapperCustom.countPriceByNumber(collect_number).toString());
        model.setStart_time(DateTimeUtil.dateToStr(collectModel.getStartTime()));
        model.setEnd_time(DateTimeUtil.dateToStr(collectModel.getEndTime()));
        model.setMyamount(myamounts);
        model.setMyTotalPrice(myTotalPrice.toString());
        model.setNewPrice(newPrices);

        model.setOrderID(orderModel==null?"":orderModel.getNumber());
        model.setOrderStatus(orderModel==null?1:orderModel.getStatus());
        model.setOrderTime(orderModel==null?"2019-2-1 18:23:36":DateTimeUtil.dateToStr(orderModel.getGmtCreate()));
        model.setRMBStr(RMBUtils.numToRMBStr(Double.valueOf(model.getTotalPrice())));
        model.setMyRMBStr(RMBUtils.numToRMBStr(myTotalPrice));

        model.setDepot_id(collectModel.getDepotId());
        model.setLogistics(collectModel.getLogistics());
        model.setDepot_id_str(deModel==null?"南阳油库":deModel.getAddress());
        model.setLogistics_str(collectModel.getLogistics()==1?"自提":"厂家配送");

        model.setListPro(list1);
        model.setListProduct(listProduct);

        return model;
    }




}
